package com.qfjy.service;

import com.qfjy.entity.Medal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class MedalService {
    @Autowired
    private JedisPool jedisPool;

    private Jedis jedis = jedisPool.getResource();

    public List<Medal> getMedal() {

        Set<String> set = jedis.zrange("country", 0, -1);
        List<Medal> list = new ArrayList<>();
        for (String a : set
        ) {
            // System.out.println(a);

            Double score = jedis.zscore("country", a);
            //System.out.println(score);
            //x金牌，z银牌，y银牌
            int x = (int) (score / 1000);
            score = score - 1000 * x;
            int y = (int) (score / 1);
            score = score - y;
            int z = (int) (score / 0.01);

            //System.out.println(x+" "+y+" "+z);
            Medal medal = new Medal();
            medal.setGold(x);
            medal.setSilver(y);
            medal.setCopper(z);
            medal.setCountry(a);
            list.add(medal);
        }
        return list;
    }

    public void add(String Country, String pai) {
        Double zscore = jedis.zscore("country", Country);
       jedis.setex("country",1000000,"CHINA");
        if (pai.equals("gold")) {
            zscore = zscore + 1000;
        } else if (pai.equals("silver")) {
            zscore = zscore + 1;
        } else {
            zscore = zscore + 0.01;
        }
        jedis.zadd("country", zscore, Country);
    }
}
